#! /usr/local/bin/ruby

require 'generator'
require File.dirname(__FILE__) + '/document'
require File.dirname(__FILE__) + '/groups_of_files'
require File.dirname(__FILE__) + '/suffix'

class DupFinder

  def initialize(min_length)
    @min_length = min_length
  end

  def get_internal_dups(input_generator)
    Suffix.match_length = @min_length

    paths = {}
    all_suffixes = []

    while input_generator.next?
      path, content = input_generator.next

      d = Document.new(content)
      suffixes = d.create_suffixes

      suffixes.each do |suffix|
        paths[suffix] ||= []
        paths[suffix] << path

        all_suffixes << suffix
      end
    end

    # To Do
    # This is a work in progress. We're coding up a new algoritm.
    all_suffixes.sort!

    # Consider using a null suffix object (or perhaps an empty one)
    # instead of nil.
    # It would never match another suffix so it would always
    # be ignored by the loop.
    # This is an example of the "Null Object" pattern.
    previous_suffix = nil

    all_suffixes.each do |suffix|
      # Compare suffix with previous_suffix
      # If they match in their first min_match_length words
      # then find out how long the match is.
      # Create two fragments, one for each suffix.
      # Create a DupText from the two fragments.
      # Look at the DupTextHash. If it has no entry for this pair of documents
      # then add one. If it has one or more DupText entries check to see whether
      # . The new DupText subsumes an existing DupText -- replace the existing DupText.
      # . The new DupText is subsumed by an existing DupText -- discard the new DupText.
      # . The new DupText neither subsumes nor is subsumed -- add it to DupTextHash.

      previous_suffix = suffix
    end

    GroupsOfFiles.new(paths)
  end
end